
package com.cysyz.web.modules.base.businesslog.service;

import com.cysyz.mylib.dao.PagingResult;
import com.cysyz.mylib.dao.sqlcondspopulater.SqlCondsPopulater;
import com.cysyz.mylib.dao.sqlcondspopulater.impl.CommonMultiCondsPopulaterImpl;
import com.cysyz.mylib.dao.type.Type;
import com.cysyz.mylib.utils.DateUtil;
import com.cysyz.web.modules.base.businesslog.domain.SysBusinessLog;
import com.cysyz.web.modules.base.support.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BusinessLogService extends BaseService {


    public void addNewBusinessLog(SysBusinessLog businessLog) {
        this.save(businessLog);
    }

    public void editBusinessLog(SysBusinessLog businessLog) {
        this.merge(SysBusinessLog.class, businessLog);
    }

    public PagingResult<SysBusinessLog> getBusinessLogList(SysBusinessLog businessLog) {

        int defaultTimeInterval = 7;
        if (businessLog.getOperateDateBegin() == null && businessLog.getOperateDateEnd() == null) {
            businessLog.setOperateDateEnd(DateUtil.getCurrentTime());
            businessLog.setOperateDateBegin(DateUtil.getDateTime(businessLog.getOperateDateEnd(), -defaultTimeInterval));
        } else if (businessLog.getOperateDateBegin() == null && businessLog.getOperateDateEnd() != null) {
            businessLog.setOperateDateBegin(DateUtil.getDateTime(businessLog.getOperateDateEnd(), -defaultTimeInterval));
        } else if (businessLog.getOperateDateBegin() != null && businessLog.getOperateDateEnd() == null) {
            businessLog.setOperateDateEnd(DateUtil.getDateTime(businessLog.getOperateDateBegin(), defaultTimeInterval));
        } else {
            int daysOfTwo = DateUtil.daysOfTwo(businessLog.getOperateDateBegin(), businessLog.getOperateDateEnd());
            if (daysOfTwo > defaultTimeInterval) {
                throw new RuntimeException("the days of beging and end must not be large than 7");
            }
        }

        StringBuilder sqlBuilder = new StringBuilder();

        sqlBuilder.append("SELECT ");
        sqlBuilder.append("  business.ID AS id17_,");
        sqlBuilder.append("  business.VERSION AS version17_,");
        sqlBuilder.append("  business.BUSINESS_LOG_TYPE AS business3_17_,");
        sqlBuilder.append("  business.EXTRA_MESSAGE AS extraMessage,");
        sqlBuilder.append("  business.OPERATE_DATE AS operateDate,");
        sqlBuilder.append("  business.OPERATION_NAME AS operationName,");
        sqlBuilder.append("  business.OPERATOR_ID AS operator7_17_ ,");
        sqlBuilder.append("  su.`LOGIN_NAME` as operatorName");
        sqlBuilder.append(" FROM");
        sqlBuilder.append("  FNM_SYS_BUSINESS_LOG business, FNM_SYS_USER su");
        sqlBuilder.append(" WHERE 1 = 1 ");
        sqlBuilder.append("	AND business.`OPERATOR_ID` = su.`ID` ");

        Map<String, Type> scalars = new HashMap<String, Type>();
        scalars.put("operationName", Type.STRING);
        scalars.put("extraMessage", Type.STRING);
        scalars.put("operateDate", Type.TIMESTAMP);
        scalars.put("operatorName", Type.STRING);

        List<Object> params = new ArrayList<>();
        SqlCondsPopulater sqlCondsPopulater = new CommonMultiCondsPopulaterImpl(sqlBuilder, params, true);

        sqlCondsPopulater.addCond("OPERATE_DATE", ">", businessLog.getOperateDateBegin());
        sqlCondsPopulater.addCond("OPERATE_DATE", "<", DateUtil.getDateTime(businessLog.getOperateDateEnd(), 1));
        sqlCondsPopulater.addCond("business.OPERATION_NAME", "%like%", businessLog.getOperationName());

        sqlBuilder.append("	order by business.OPERATE_DATE DESC ");

        return listPaging(sqlBuilder.toString(), params, SysBusinessLog.class, scalars, businessLog.getPage(), businessLog.getMaxResult());

    }
}
